/**
 */
package com.dingpeikeji.quickdeer.core.modules.returnrule.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.core.modules.returnrule.dao.TReturnruleDtlDao;
import com.dingpeikeji.quickdeer.core.modules.returnrule.entity.TReturnrule;
import com.dingpeikeji.quickdeer.core.modules.returnrule.service.TReturnruleService;

/**
 * 退票规则定义Controller
 * @author lh
 * @version 2016-07-08
 */
@Controller
@RequestMapping(value = "${adminPath}/returnrule/tReturnrule")
public class TReturnruleController extends BaseController {

	@Autowired
	private TReturnruleService tReturnruleService;
	@Autowired
	private TReturnruleDtlDao tReturnruleDtlDao;
	
	@ModelAttribute
	public TReturnrule get(@RequestParam(required=false) String returnid) {
		TReturnrule entity = null;
		if (StringUtils.isNotBlank(returnid)){
			entity = tReturnruleService.get(returnid);
		}
		if (entity == null){
			entity = new TReturnrule();
		}
		return entity;
	}
	
	@RequiresPermissions("returnrule:tReturnrule:view")
	@RequestMapping(value = {"list", ""})
	public String list(TReturnrule tReturnrule, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TReturnrule> page = tReturnruleService.findPage(new Page<TReturnrule>(request, response), tReturnrule); 
		model.addAttribute("page", page);
		model.addAttribute("tReturnrule", tReturnrule);
		return "modules/returnrule/tReturnruleList";
	}

	@RequiresPermissions("returnrule:tReturnrule:view")
	@RequestMapping(value = "form")
	public String form(TReturnrule tReturnrule, Model model) {
		model.addAttribute("tReturnrule", tReturnrule);
		model.addAttribute("rtnrulenum", tReturnrule.gettReturnruleDtlList().size());
		return "modules/returnrule/tReturnruleForm";
	}

	@RequiresPermissions("returnrule:tReturnrule:edit")
	@RequestMapping(value = "save")
	public String save(TReturnrule tReturnrule, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tReturnrule)){
			return form(tReturnrule, model);
		}
		tReturnruleService.save(tReturnrule);
		addMessage(redirectAttributes, "保存退票规则定义成功");
		return "redirect:"+Global.getAdminPath()+"/returnrule/tReturnrule/?repage";
	}
	
	@RequiresPermissions("returnrule:tReturnrule:edit")
	@RequestMapping(value = "delete")
	public String delete(TReturnrule tReturnrule, RedirectAttributes redirectAttributes) {
		tReturnruleService.delete(tReturnrule);
		addMessage(redirectAttributes, "删除退票规则定义成功");
		return "redirect:"+Global.getAdminPath()+"/returnrule/tReturnrule/?repage";
	}

	/**
	 * 验证退票规则名称是否唯一
	 * @param oldRtnrulename
	 * @param rtnrulename
	 * @return
	 */
	@ResponseBody
	@RequiresPermissions("returnrule:tReturnrule:edit")
	@RequestMapping(value = "checkRtnrulename")
	public String checkRtnrulename(String oldRtnrulename, String rtnrulename) {
		
		if (rtnrulename !=null && rtnrulename.equals(oldRtnrulename)) {
			return "true";
		} else if (rtnrulename !=null && tReturnruleService.getReturnruleByRtnrulename(rtnrulename) == null) {
			return "true";
		}
		return "false";
	}
}
